
#include <cstdlib>
#include <iostream>

#include "common.h"

#include "SDL.h"
#include "SDL_opengl.h"
#include "Drumstick.h"
#include "LogUtil.h"

using namespace std;

__BEGIN_NAMESPACE_DECL_

// DrumstickLocation Ctor
DrumstickLocation::DrumstickLocation(double x, double y, double z, 
                                     double rotX, double rotY, double rotZ)
    : _x(x), _y(y), _z(z), _rotX(rotX), _rotY(rotY), _rotZ(rotZ)
{
}

// Constructor
Drumstick::Drumstick()
    : _displayList(0), _aor(0), _inMotion(false)
{
}

// Destructor
Drumstick::~Drumstick()
{
}

// addLocation
void
Drumstick::addLocation (std::string locationName, 
                        const DrumstickLocation& location)
{
    _locationMap[locationName] = location;
}

// removeLocation
bool
Drumstick::removeLocation (std::string locationName)
{
    std::map<std::string, DrumstickLocation>::iterator iter;

    iter = _locationMap.find(locationName);
    if (iter != _locationMap.end())
    {
        _locationMap.erase(iter);
        return true;
    }

    return false;
}

// setLocation
bool 
Drumstick::setLocation (std::string locationName)
{
    std::map<std::string, DrumstickLocation>::iterator iter;

    iter = _locationMap.find(locationName);
    if (iter != _locationMap.end())
    {
        _location = _locationMap[locationName];
        return true;
    } 

    return false;
}

double Drumstick::getLocationX() const { return _location._x; }
double Drumstick::getLocationY() const { return _location._y; }
double Drumstick::getLocationZ() const { return _location._z; }
double Drumstick::getLocationRotX() const { return _location._rotX; }
double Drumstick::getLocationRotY() const { return _location._rotY; }
double Drumstick::getLocationRotZ() const { return _location._rotZ; }


// setDisplayList
void
Drumstick::setDisplayList(int displayList)
{
    _displayList = displayList;
}

// getDisplayList
int
Drumstick::getDisplayList()
{
    return _displayList;
}

// setInMotion
void
Drumstick::setInMotion()
{
    _inMotion = true;
}

// isInMotion
bool
Drumstick::isInMotion() const
{
    return _inMotion;
}

void
Drumstick::advanceState()
{
    // state machine for drumstick 1
    if (_aor == 60) _aor = 40;
    else if (_aor == 40) _aor = 20;
    else if (_aor == 20) { _aor = 0; _inMotion = false; }
    else if (_inMotion && _aor == 0) _aor = 19;
    else if (_aor == 19) _aor = 39;
    else if (_aor == 39) _aor = 60;
}

void
Drumstick::draw()
{
    // Save current matrix
    //glPushMatrix();
    glTranslated (_location._x, _location._y, _location._z);
    glPushMatrix();
      glRotated (_location._rotZ, 0.0, 0.0, 1.0);
      glPushMatrix();
        glRotated (_location._rotY, 0.0, 1.0, 0.0);
        glPushMatrix();
          glRotated (_location._rotX, 1.0, 0.0, 0.0);
          glRotated (90-_aor, 1.0, 0.0, 0.0);
          //glRotated (90, 1.0, 0.0, 0.0);
          // Apply any translation, rotation or scaling factors set for this object
          transform();
          // Draw the object
          if (!glIsList(_displayList))
              ERROR << "Bad display list";
          else
              glCallList(_displayList);
        glPopMatrix();
      glPopMatrix();
    glPopMatrix();
    // Restore matrix
    //glPopMatrix();
}

__END_NAMESPACE_DECL_
